package com.big.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.big.app.dao.AttentionDao;
import com.big.app.entity.Attention;
import com.big.app.entity.Messages;
import com.big.app.service.intf.AttentionService;
import com.big.app.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.big.app.config.ZanCangConfig.ATTENTION_ZACTIVE_SUCCESS;


/**
 * @author: zjl
 * @description username即为登录用户唯一标识,可设置变量接收该标识,即为动态.
 * @date: 2022/4/21 20:56
 */
@Service
public class AttentionServiceImpl implements AttentionService {
    @Resource
    private AttentionDao attentionDao;
    @Override
    public R insert(Integer mid, Integer zactive) {
        Attention attention =new Attention();
        attention.setUsername("zjl");
        attention.setMid(mid);
        attention.setZactive(zactive);
        if (attentionDao.selectFlag(mid,"zjl")==null){
            if (attentionDao.insert(attention)>0){
                return new R (200,"ok",attentionDao.selectFlag(mid,"zjl"));
            }
        }else{
            UpdateWrapper<Attention> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("mid",mid).eq("username","zjl");
            attention.setZactive(zactive);
            if (attentionDao.update(attention, updateWrapper)>0){
                return new R(200,"ok",attentionDao.selectFlag(mid,"zjl"));
            }
        }

        return new R(400,"fail",null);
    }

    @Override
    public R updateZactive(Integer mid, Integer zactive) {
        UpdateWrapper<Attention> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("mid",mid).eq("username","zjl");
        Attention attention = new Attention();
        attention.setZactive(zactive);
        if (attentionDao.update(attention, updateWrapper)>0){
            return new R(200,"ok",attentionDao.selectFlag(mid,"zjl"));
        }
        return new R(400,"fail",null);
    }

    @Override
    public R selectAll(Integer mid) {
        Attention attention =new Attention();
        attention.setUsername("zjl");
        if (attentionDao.selectFlag(mid,"zjl")!=null){
            return new R(200,"ok",attentionDao.selectFlag(mid,"zjl"));
        }
        return new R(400,"fail",null);
    }

    @Override
    public R All() {
        if (attentionDao.selectMid(ATTENTION_ZACTIVE_SUCCESS,"zjl")!=null){
            List<Messages> ll = new ArrayList<>();
             attentionDao.selectMid(ATTENTION_ZACTIVE_SUCCESS,"zjl").forEach(t->
                ll.add(attentionDao.selectAll(t.getMid()))
             );
            System.err.println(ll);
            return new R(200,"ok",ll);
        }
        return new R(400,"fail",null);
    }

}
